//
//  FrameSelectionViewController.m
//  HpnotiqCustomize
//
//  Created by Harmandeep Singh on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FrameSelectionViewController.h"
#import "ModifyPhotoViewController.h"
#import "FrameView.h"



@interface FrameSelectionViewController()

- (void)showTempFrame;
- (void)temp:(UITapGestureRecognizer *)gesture;

@end




@implementation FrameSelectionViewController

@synthesize mOriginalImage;
@synthesize mImageIsShotFromApp;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    self.title = @"Frame Selection";
    
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:nil] autorelease];
    
    
    [self showTempFrame];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    

    [mOriginalImage release];
    [super dealloc];
}








- (void)showTempFrame
{
    
    CGFloat widthForFrameView = 0;
    CGFloat heightForFrameView = 0;
    
    CGFloat imageRatio = mOriginalImage.size.width / mOriginalImage.size.height;
    
    if(imageRatio >= 1)
    {
        // Landscape Photo
        
        widthForFrameView = self.view.frame.size.width;
        heightForFrameView = widthForFrameView / imageRatio;
    }
    else
    {
        // Portrait Photo
        
        heightForFrameView = self.view.frame.size.height;
        widthForFrameView = imageRatio * heightForFrameView;
    }
    
    
    UIImage *frameImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SampleFrame" ofType:@"png"]];
    
    
    
    CGRect frameToBeUsed = CGRectMake(roundf((self.view.frame.size.width - widthForFrameView) / 2),
                                      roundf((self.view.frame.size.height - heightForFrameView) / 2),
                                      widthForFrameView,
                                      heightForFrameView);
    
    
    
    FrameView *frameView = [[FrameView alloc] initWithFrame:frameToBeUsed withSelectedImage:mOriginalImage andFrameImage:frameImage];
    [self.view addSubview:frameView];
    [frameView release];
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(temp:)];
    [frameView addGestureRecognizer:tapGesture];
    [tapGesture release];
}





- (void)temp:(UITapGestureRecognizer *)gesture
{
    ModifyPhotoViewController *viewController = [[ModifyPhotoViewController alloc] initWithNibName:@"ModifyPhotoViewController" bundle:nil];
    viewController.mOriginalImage = self.mOriginalImage;
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}


@end
